﻿using MESPro.DBManager;
using MESPro.Enums;
using MESPro.Global;
using MESPro.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace MESPro.Views
{
    public partial class ModelListFrm : DockContent
    {

        /// <summary>
        /// ListView选择项改变后触发
        /// </summary>
        public Action<ListViewItem?>? RMListViewItemSelected { get; set; }
        /// <summary>
        /// 提交到数据库后,需要刷新界面
        /// </summary>
        public Action? RMUpdateableByObject { get; set; }
        /// <summary>
        /// 发生异常后触发的动作
        /// </summary>
        public Action<object, MsgLevel>? RMShowMsg { get; set; }
        private List<Model>? modelList;
        /// <summary>
        /// 所有的模块
        /// </summary>
        public List<Model>? RMModelList
        {
            get => modelList;
            set
            {
                modelList = value;
                this.Invoke(() => { 等到数据后刷新界面(); });
            }
        }
        public ModelListFrm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        public void 等到数据后刷新界面()
        {
            this.listView1.Clear();
            if (this.RMModelList == null) return;
            foreach (Model item in RMModelList)
            {
                ListViewItem listViewItem1 = new ListViewItem(item.DisLanguage.GetDisplayName(), item.ImageKey);
                listViewItem1.Tag = item;
                this.listView1.Items.Add(listViewItem1);
            }
        }

        private void AddModel(object sender, EventArgs e)
        {
            ModelManager modelManager = new ModelManager();
            var res = modelManager.AddModel(new Model() { DisLanguage = new DisLanguage() { zh_CN = $"模块{Guid.NewGuid()}", en_US = $"Model{Guid.NewGuid()}" } });
            RMShowMsg?.Invoke(res, MsgLevel.Trace);
            RMUpdateableByObject?.Invoke();
            RMListViewItemSelected?.Invoke(null);
        }

        private void DeleteModel(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems == null)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            if (this.listView1.SelectedItems.Count < 1)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            if (this.listView1.SelectedItems[0].Tag == null)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            var item = this.listView1.SelectedItems[0];
            var obj = this.listView1.SelectedItems[0].Tag;

            var res = DbContext.Db.DeleteableByObject(obj).ExecuteCommand();
            RMShowMsg?.Invoke(res, MsgLevel.Trace);
            RMUpdateableByObject?.Invoke();
            RMListViewItemSelected?.Invoke(null);

        }
        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            RMListViewItemSelected?.Invoke(e.Item);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems == null)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            if (this.listView1.SelectedItems.Count < 1)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            if (this.listView1.SelectedItems[0].Tag == null) 
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            } 
            var objmodel = this.listView1.SelectedItems[0].Tag as Model;
            if (objmodel == null) return;
            var noadd = DbContext.Db.Queryable<FunctionGroup>()
                            .Where(x => !objmodel.FunctionGroups.Any(y => y.Id == x.Id))
                            .ToList();
            AddFunctionGroup frm = new AddFunctionGroup(objmodel);
            frm.ShowDialog();
            RMUpdateableByObject?.Invoke();
        }
    }
}
